import Vue from 'vue'
import Router from 'vue-router'
import store from "../store/index.js";
import HelloWorld from '@/components/HelloWorld'
import Entry from '../views/entry.vue'
import AccountInfo from '@/views/Account'
import Product from '@/views/product'
import Login from '@/views/login'
import EcharsView from '../views/echartsView/index.vue'
import QrcodeView from '@/views/qrcodeView'

Vue.use(Router)

let router = new Router({
  routes: [
    // {
    //   path: '/',
    //   name: 'HelloWorld',
    //   component: HelloWorld
    // },
    {
      path: "/",
      name: "qrcodeView",
      component: QrcodeView
    },
    {
      path: "/echars",
      name: "echarsView",
      component: EcharsView
    },
    {
      // path: "/",
      path: "/login",
      name: "login",
      component: Login
    },
    {
      path: '/entry',
      name: 'entry',
      redirect: "/accountInfo",
      component: Entry,
      // 隐藏路由器地址
      // hidden: true,
      children: [
        {
          path: '/accountInfo',
          name: 'accountInfo',
          meta: {
            requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
          },
          component: AccountInfo
        },
        {
          path: '/product',
          name: 'product',
          meta: {
            requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
          },
          component: Product
        },
      ]
    }
  ]
});
// /**
//  * 刷新islogin的值
//  */
// if (window.sessionStorage.getItem("islogin")) {
//   store.commit({
//     type: "islogin",
//     flag: window.sessionStorage.getItem("islogin")
//   });
// }
/**
 * 前置守卫
 */
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    // 判断该路由是否需要登录权限
    if (store.getters.islogin) {
      //判断本地是否存在access_token
      // sessionStorage.removeItem("history_path");
      //表示已经登录
      next();
    }  else {
      next({
        name: "login"
      });
    }
  } else {
    next();
  }

  /*如果本地 存在 token 则 不允许直接跳转到 登录页面*/
  if (to.fullPath == "/") {
    if (store.getters.islogin) {
      next({
        path: from.fullPath
      });
    } else {
      next();
    }
  }
});

export default router;